# -*- coding:utf-8 -*-
from os import path
from dotenv import load_dotenv, find_dotenv
from pydantic import BaseConfig
from typing import List
from functools import lru_cache

ROOT_PATH = path.dirname(path.dirname(path.abspath(path.join(__file__, '.'))))


class Config(BaseConfig):
    # 加载环境变量
    load_dotenv(find_dotenv(), override=True)

    # 调试模式
    APP_DEBUG: bool = True

    # 项目信息
    VERSION: str = "1.0.1"
    PROJECT_NAME: str = "FastApiAdmin"
    DESCRIPTION: str = 'FastApiAdmin后台框架开源且可以免费商用，一键生成 CRUD， FastApiAdmin是一款基于 Python、FastApi、Vue3 的极速后台开发框架。'
    ROOT_PATH: str = ROOT_PATH
    UPLOAD_PATH: str = path.join(ROOT_PATH, 'uploads')

    # 路径常量 (最后不带/)
    # ROOT_PATH
    APP_PATH = ROOT_PATH + "/app"
    RUNTIME_PATH = ROOT_PATH + "/runtime"
    LOG_PATH = RUNTIME_PATH + "/log"
    CACHE_PATH = RUNTIME_PATH + "/cache"
    TEMP_PATH = RUNTIME_PATH + "/temp"

    # 跨域请求
    CORS_ORIGINS: List = ["*"]
    CORS_ALLOW_CREDENTIALS: bool = True
    CORS_ALLOW_METHODS: List = ["GET", "POST"]
    CORS_ALLOW_HEADERS: List = ["*"]

    # 允许的 hosts 列表
    AllowHosts = ["127.0.0.1", "localhost"]

    # DateTime
    DATETIME_TIMEZONE: str = 'Asia/Shanghai'
    DATETIME_FORMAT: str = '%Y-%m-%d %H:%M:%S'

    # 默认请求超时
    REQUEST_TIMEOUT: int = 60

    # 上传文件路径
    upload_directory: str = ROOT_PATH + '/static/uploads'
    # 上传图片限制
    upload_image_size = 1024 * 1024 * 10
    # 上传视频限制
    upload_video_size = 1024 * 1024 * 30
    # 上传图片扩展
    upload_image_ext = {'png', 'jpg', 'jpeg', 'gif', 'ico', 'bmp'}
    # 上传视频扩展
    upload_video_ext = {'mp4', 'mp3', 'avi', 'flv', 'rmvb', 'mov'}
    # 上传路径URL前缀
    upload_prefix = '/api/common/uploads'

    # 系统
    # 登录失败超过5次则1天后重试 未实现
    LOGIN_FAILURE_RETRY: bool = True
    LOGIN_FAILURE_LIMIT: int = 5
    LOGIN_FAILURE_PREVTIME: int = 3600
    # 是否同一账号同一时间只能在一个地方登录 未实现
    LOGIN_UNIQUE: bool = True  # 目前默认就是这样，暂未启用
    # 是否开启IP变动检测 未实现
    LOGIN_IP_CHECK: bool = False  # 暂未启用

    # 验证码长度
    CAPTCHA_TYPE: str = 'text'
    CAPTCHA_LENGTH: int = 4
    CAPTCHA_ENABLE: bool = True
    CAPTCHA_EXPIRE: int = 300
    CAPTCHA_PREFIX: str = 'captcha:'

    # Token相关参数
    TOKEN_PREFIX: str = 'access_token:'
    TOKEN_EXPIRE: int = 60 * 60 * 24 * 7
    TOKEN_SECRET: str = '<KEY>'

    # 排除认证接口
    SECURITY_EXCLUDES: List[str] = ['/api/sys/auth/captcha', '/api/sys/auth/login', '/api/sys/auth/logout', '/api/sys/auth/client/list']
@lru_cache()
def get_config():
    return Config()
